﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Assessment.Management.Utils;
using Assessment.Management.SharedComponent.Params.ManagerParam;
using Assessment.Management.SharedComponent.Constants;
using Assessment.Management.BIZ;
using Assessment.Management.SharedComponent.Entities;
using System.IO;
using System.Data.OleDb;
using System.Data.SqlClient;
using Assessment.Management.UI.Admin.Upload;

namespace Assessment.Management.UI.Admin.UserControls.Teachers
{
    public partial class TeacherAdd : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DepartmentParam param = new DepartmentParam(FunctionType.GetListDepartment);
            MainController.Provider.Execute(param);
            ddlKhoa.DataSource = param.listDepartment;
            ddlKhoa.DataBind();
            ddlKhoa.Items.Insert(0, new ListItem("----------Chọn Khoa----------", ""));
            ddlKhoa.SelectedIndex = 0;
        }
        private bool ValidateDataToAdd()
        {
            bool kt = true;

            if (txtAdress.Text==""||txtEmail.Text==""||txtMaGV.Text==""||txtMobile.Text==""||txtTenGV.Text=="")
            {
                kt = false;
            }
            return kt;
        }
        private bool CheckExitsData()
        {
            TeacherParam param = new TeacherParam(FunctionType.CheckExitsData);
            Teacher teacher = new Teacher();
            teacher.ID = int.Parse(txtMaGV.Text);
            param.Teacher = teacher;
            MainController.Provider.Execute(param);
            bool kt = false;
            if (param.Teacher.ID>0)
            {
                kt = true;
            }
            return kt;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
              if (ValidateDataToAdd())
            {
                if (CheckExitsData())
                {
                    lblMessage.Text = "<font color='Red'>Đã tồn tại dữ liệu này. Vui lòng kiểm tra lại</font>";
                }
                else
                {
            SaveTeacher();
                }

            }
              else
              {
                  lblMessage.Text = "<font color='Red'>Thông tin chưa đầy đủ. Vui lòng kiểm tra lại</font>";
              }
        }
        protected void btnSaveTeacherAndNew_Click(object sender, EventArgs e)
        {
             if (ValidateDataToAdd())
            {
                if (CheckExitsData())
                {
                    lblMessage.Text = "<font color='Red'>Đã tồn tại dữ liệu này. Vui lòng kiểm tra lại</font>";
                }
                else
                {
            SaveTeacherAndNew();
                }

            }
             else
             {
                 lblMessage.Text = "<font color='Red'>Thông tin chưa đầy đủ. Vui lòng kiểm tra lại</font>";
             }
        }
        protected void btnExit_Click(object sender, EventArgs e)
        {
            Response.Redirect("UniAdmin.aspx?do=teachermanager");
        }


        //Lưu môn học xuống csdl
        private void SaveTeacher()
        {
            TeacherParam param = new TeacherParam(FunctionType.CreateTeacher);
            param.Teacher = GetItemOnForm();
            MainController.Provider.Execute(param);
            Response.Redirect("UniAdmin.aspx?do=teachermannager");
        }
        private void SaveTeacherAndNew()
        {
            TeacherParam param = new TeacherParam(FunctionType.CreateTeacher);
            param.Teacher = GetItemOnForm();
            MainController.Provider.Execute(param);
            Response.Redirect("UniAdmin.aspx?do=teacheradd");
        }

        //Lấy dữ liệu nhập vào
        private Teacher GetItemOnForm()
        {

            Teacher teacher = new Teacher();
            teacher.TeacherName = txtTenGV.Text.Trim();
            teacher.DepartmentID = int.Parse(ddlKhoa.SelectedValue);
            teacher.Address = txtAdress.Text.Trim();
            teacher.Mobile = txtMobile.Text.Trim();
            teacher.Email = txtEmail.Text.Trim();
            teacher.CreatedBy = SMX.smx_CreatedBy;
            teacher.CreatedDTG = DateTime.Now;
            teacher.Deleted = SMX.smx_IsNotDeleted;
            teacher.Version = SMX.smx_FirstVersion;
            return teacher;
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (fileUploadGV.HasFile)
            {

                string path = string.Concat(Server.MapPath(@"\UI\Admin\Upload\" + fileUploadGV.FileName));
                FileInfo fInfo = new FileInfo(path);
                if (fInfo.Exists)
                {
                    fInfo.Delete();
                }
                fileUploadGV.SaveAs(path);
                           Uploadfile up = new Uploadfile();
                up.Upload(path, "Teachers");
                Response.Redirect("UniAdmin.aspx?do=teachermanager");
            }
        }
    }
}